/*
import {Component, Inject, OnInit, Optional} from '@angular/core';
import {_HttpClient} from '@delon/theme';
import {MenuItem, TreeNode} from 'primeng/api';
import {UtilityService} from '../../../service/utils.service';
import {Router} from '@angular/router';
// 公共接口名
import {appConfig} from '../../../service/common';
import {OrgModule} from '../../../service/common.module';
import {NzModalService, NzNotificationService} from 'ng-zorro-antd';
import {ReuseTabService} from '@delon/abc';
import {CommondictServe} from '../../../service/commonDict';
import { ActivatedRoute } from '@angular/router';

@Component({
  selector: 'app-org',
  templateUrl: './org.component.html',
  styleUrls: ['./org.component.less']
})
export class OrgComponent implements OnInit {
    constructor(
        private http: _HttpClient,
        private router: Router,
        private utilityService: UtilityService,
        private modal: NzModalService,
        private nznot: NzNotificationService,
        private commondict: CommondictServe,
        private activatedRoute: ActivatedRoute, // 注入路由，接收到参数
        @Optional() @Inject(ReuseTabService) private reuseTabService: ReuseTabService,
    ) { }

    treedata: any[]; // tree组件数据
    treemenus: MenuItem[];
    searchTitle: string; // 树搜索框文字
    tabShow: boolean;
    id: string;
    orgName: string; // 组织机构名称
    modalVisible = false; // 弹出框默认关闭
    isEdit = false; // 默认是新增
    orgData: any; // 树节点上的数据保存
    istrue = false;  // 是否请求过
    orgItem: OrgModule = new OrgModule(); // 绑定数据
    // 枚举值
    orgDegree: any;
    area: any;
    orgType: any;
    treeResult: string; // 接受请求id值
    isroot = true; // 是否是子节点调用
    dataOrg: any; // 节点信息
    selectionType: string; // 树结构类型
    orgTitle: string;
    // 布局样式
    widthLeft = 7;
    widthRight = 17;
    isShow = true;
    loading = false;
    text = '收起'
    selectIndex = 0; // tab索引初始化
    // 自定义卡片收缩方法
    toggleCollapsed(): void {
        this.isShow = !this.isShow;
        if(this.isShow === true){
            this.widthLeft = 7;
            this.widthRight = 17;
            this.text = '收起'
        }else{
            this.widthLeft = 2;
            this.widthRight = 22;
            this.text = '展开'
        }
    }
    // tabs标题
    tabs = [
        {
            name   : '员工',
            router : 'org/emp'
        },
        {
            name   : '岗位',
            router : 'org/post'
        }
    ];


    ngOnInit() {
        this.initData();
        this.searchTitle = '请输入机构代码/名称';
        // 枚举值转换
        this.selectionType = 'single';
        this.reuseTabService.title = '组织机构'
        if(this.reuseTabService.items) {
            if(this.reuseTabService.items.length > 0) {
                for(let i = 0; i < this.reuseTabService.items.length; i ++) {
                    if(this.reuseTabService.items[i].title !== '组织机构') { // 没有才添加
                        /!* 把当前路由添加到 tab 缓存中*!/
                        this.reuseTabService.store(this.activatedRoute.snapshot, '')
                    }
                }
            } else {
                this.reuseTabService.store(this.activatedRoute.snapshot, '')
            }
        }

        this.orgName = '组织机构'
        this.dictQueryKey()
    }



    // 根据key查询业务字典
    dictQueryKey() {
        this.commondict.queryDictKey(appConfig.dictKeyList.dictsdArea)
            .subscribe((val) => {
                this.area = val.result;
            })

        this.commondict.queryDictKey(appConfig.dictKeyList.orgType)
            .subscribe((val) => {
                this.orgType = val.result;
            })
        this.commondict.queryDictKey(appConfig.dictKeyList.dictOrgde)
            .subscribe((val) => {
                this.orgDegree = val.result;
            })
    }



    initData() {
        this.treedata = [ // 默认根节点
            {
                'label': '组织机构',
                'data': 'Documents Folder',
                'guid': 'null',
                'expandedIcon': 'fa fa-institution',
                'collapsedIcon': 'fa fa-institution',
                'children': [{}]
            }
        ];
        // 从服务器获取树列表
        this.utilityService.getData(appConfig.serverUrl  + appConfig.API.omgTree)
            .subscribe(
                (val) => {

                    for (let i = 0 ; i < val.result.length; i++) {
                        if (val.result[i].isleaf === '是') { // 代表是最底层，没有下级了
                            val.result[i].collapsedIcon = 'anticon anticon-usb';
                        } else {
                            val.result[i].expandedIcon = 'fa fa-institution';
                            val.result[i].collapsedIcon = 'fa fa-institution';
                            val.result[i].children = val.result[i].childrens;
                            this.iteration(val.result[i].children);
                        }

                    }
                    this.treedata[0].children = val.result;
                    this.treedata.forEach( node => {
                        this.expandRecursive(node, true);
                    });
                });
    }



    iteration(node) {
        for (let i = 0; i < node.length; i++) {
            if (node[i].childrens !== undefined && node[i].childrens.length > 0) {
                console.log(node[i])
                node[i].expandedIcon = 'fa fa-institution';
                node[i].collapsedIcon = 'fa fa-institution';
                node[i].children = node[i].childrens; // 重新赋值
                this.iteration(node[i].children); // 继续调用
            } else { // 字典项
                node[i].collapsedIcon = 'anticon anticon-usb';
                node[i].expandedIcon = 'anticon anticon-usb';
            }
        }
    }

    getData(event) {
        if (event.guid !== 'null') {
            // 从服务器获取树列表
            this.utilityService.postData(appConfig.serverUrl  + appConfig.API.omgTree + '/' + event.guid, {})
                .subscribe(
                    (val) => {
                        for (let i = 0 ; i < val.result.children.length; i++) {
                            if (val.result.children[i].isleaf === '是') { // 代表是最底层，没有下级了
                                val.result.children[i].collapsedIcon = 'anticon anticon-usb';
                            } else {
                                // val.result.children[i].label = val.result.children[i].orgName;
                                val.result.children[i].expandedIcon = 'fa fa-institution';
                                val.result.children[i].collapsedIcon = 'fa fa-institution';
                                val.result.children[i].children =  [{}];
                            }

                        }
                        event.children = val.result.children;
                    });
        }


    }




    // 树节点搜索框的内容
    searchVal($event) {
        let jsonObj = {
            guid: 'null',
            name: $event
        }
        this.utilityService.postData(appConfig.serverUrl  + appConfig.API.orgTreeFilter, jsonObj)
            .subscribe(
                (val) => {
                    this.treedata = [ // 默认根节点
                        {
                            'label': '组织机构',
                            'data': 'Documents Folder',
                            'guid': 'null',
                            'expandedIcon': 'fa fa-institution',
                            'collapsedIcon': 'fa fa-institution',
                            'children': [{}]
                        }
                    ];
                    this.treedata[0].children = val.result.children;
                    for(let i = 0 ; i < val.result.children.length; i++) {
                        this.getData(val.result.children[i]);
                    }
                    // 展开所有的
                    this.treedata.forEach( node => {
                        this.expandRecursive(node, true);
                    } );

                },
            );
    }

    // 树的方法

    // 展开节点事件
    Unfold(event) {
        this.getData(event.node);
    }


    // 右击菜单传递值
    RightSelect(event) {
        console.log(event.node);
        if (event.node.guid === 'null') { // 子业务字典
            this.treemenus = [
                {label: '新建根机构', icon: 'fa fa-plus', id: 'Opr003002', command: (event) => this.addrootOrg()},
            ];
        } else {  // 字典项
            this.treemenus = [
                {label: '新增子机构', icon: 'fa fa-plus',id: 'Opr003003', command: (event) => this.addchildOrg()},
                {label: '修改机构', icon: 'fa fa-edit', id: 'Opr003004', command: (event) => this.editOrg()},
                {label: '删除机构', icon: 'fa fa-times', id: 'Opr003005', command: (event) => this.delectOrg()},
            ];
        }
        delete event.node.parent;
        this.orgData = event.node; // 绑定数据
    }

    // 左击树菜单节点信息
    TreeSelect(event) {
        this.id = event.node.guid;
        this.orgName = event.node.label;
        if  (event.node.guid !== 'null') { // 只要不是跟机构就显示
            this.tabShow = true;
            this.selectIndex  = 0
            this.router.navigate(['org/emp', this.id]); // 跳转路由
        } else {
            this.selectIndex  = 0
            this.router.navigate(['org']);
            this.tabShow = false;
        }
    }
    // 拖拽方法
    dropEvent($event) {
        console.log($event)
        let originPonit = $event.dragNode.guid;
        let destinaPoint = $event.dropNode.guid;
        // 如果拖拽的目的地菜单是叶子菜单，拖拽目标目的地设为它的父菜单
        if ($event.dropNode.isleaf === 'Y') {
            destinaPoint = $event.dropNode.guidParents;
        }
        let  playOrder = $event.dragNode.displayOrder;
        let moveDate = {
            'targetGuid': destinaPoint,
            'moveGuid': originPonit,
            'order': 0
        };
        this.utilityService.postData(appConfig.serverUrl  + appConfig.API.queryMoveOrgLists , moveDate )
            .subscribe(
                (val) => {
                    this.initData()
                    this.nznot.create('success', '拖拽成功' , '拖拽成功');
                },
                (err) => {
                    // this.getData($event.dropNode); // 查询目标下的子集
                    this.initData()
                    this.nznot.create('error', err.msg , err.msg);
                }
            );
    }




    // 展开所有树节点的方法
    private expandRecursive(node:TreeNode, isExpand:boolean){
        node.expanded = isExpand;
        if(node.children){
            node.children.forEach( childNode => {
                this.expandRecursive(childNode, isExpand);
            } );
        }
    }

    // 新增跟组织机构
    addrootOrg() {
        this.isroot = true;
        this.isEdit = false;
        this.orgTitle = '新增组织机构'
        this.orgItem = new OrgModule(); // 清空
        this.modalVisible = true;
    }
    // 新建子组织机构
    addchildOrg() {
        this.orgItem = new OrgModule(); // 清空
        this.modalVisible = true;
        this.orgTitle = '新增组织机构'
        this.isEdit = false;
        this.isroot = false;
    }


    // 枚举值转换
    // 机构等级转换
    degreeOrg(event) {
        if (event.orgDegree === '总行') {
            event.orgDegree = 'BS';
        } else if (event.orgDegree === '分行') {
            event.orgDegree = 'YF';
        } else if (event.orgDegree === '海外') {
            event.orgDegree = 'HW';
        } else if (event.orgDegree === '区域分行') {
            event.orgDegree = 'QY';
        } else if (event.orgType === '网点') {
            event.orgDegree = 'CN';
        }
    }
    // 机构类型转换
    typeOrg(event) {
        if (event.orgType === '总公司') {
            event.orgType = '10';
        } else if (event.orgType === '总部部门') {
            event.orgType = '11';
        } else if (event.orgType === '分公司') {
            event.orgType = '20';
        } else if (event.orgType === '分公司部门') {
            event.orgType = '21';
        } else if (event.orgType === '营业网点') {
            event.orgType = '90';
        }
    }
    //  地区
    arearOrg(event) {
        if (event.area === '北京') {
            event.area = '010';
        } else if (event.area === '上海') {
            event.area = '021';
        }
    }
    // 是否
    isleaf(event) {
        if (event.isleaf === '是') {
            event.isleaf = 'Y';
        } else if (event.isleaf === '否') {
            event.isleaf = 'N';
        }
    }
    // 机构状态
    orgStatus(event) {
        if (event.orgStatus === '正常') {
            event.orgStatus = 'running';
        } else if (event.orgStatus === '注销') {
            event.orgStatus = 'cancel';
        } else if (event.orgStatus === '停用') {
            event.orgStatus = 'stop';
        }
    }

    // 保存接口
    save() {
        const jsonOption = this.orgItem;
        if (!this.isEdit) {
            if (this.isroot) { // 新增跟机构
                this.utilityService.postData(appConfig.serverUrl  + appConfig.API.addRoot, jsonOption)
                    .subscribe(
                        (val) => {
                            this.initData()
                            this.nznot.create('success', val.msg , val.msg);
                        },
                        (error) => {
                            this.nznot.create('error', error.msg , error.msg);
                        }
                    );
            } else {
                jsonOption.guidParents = this.orgData.guid; // 绑定父机构的guid
                this.utilityService.postData(appConfig.serverUrl  + appConfig.API.addChild, jsonOption)
                    .subscribe(
                        (val) => {
                            this.initData()
                            this.nznot.create('success', val.msg , val.msg);
                        },
                        (error) => {
                            this.nznot.create('error', error.msg , error.msg);
                        }
                    );
            }
        } else {
            this.degreeOrg(jsonOption)
            this.typeOrg(jsonOption)
            this.arearOrg(jsonOption)
            this.isleaf(jsonOption)
            this.orgStatus(jsonOption)
            console.log(jsonOption)
            this.utilityService.putData(appConfig.serverUrl  + appConfig.API.omg, jsonOption)
                .subscribe(
                    (val) => {
                        this.initData()
                        this.nznot.create('success', val.msg , val.msg);
                    },
                    (error) => {
                        this.nznot.create('error', error.msg , error.msg);
                    }
                );
        }

        this.modalVisible = false;
    }


    // 修改组织机构
    editOrg() {
        this.isEdit = true; // 是修改
        this.modalVisible = true;
        this.orgTitle = '修改组织机构'
        // 查询修改的数据信息
        this.utilityService.getData(appConfig.serverUrl + appConfig.API.omg + '/' +  this.orgData.guid)
            .subscribe(
                (val) => {
                    this.dataOrg = val.result;
                    this.degreeOrg(val.result)
                    this.typeOrg(val.result)
                    this.arearOrg(val.result)
                    this.orgItem = val.result; // 渲染数据
                });

    }


    // 删除组织机构
    delectOrg() {
        this.modal.open({
            title: '是否删除',
            content: '您确认要删除该机构吗? 删除该工作组下所有子机构都会被一并删除',
            okText: '确定',
            cancelText: '取消',
            onOk: () => {
                this.utilityService.deleatData(appConfig.serverUrl + appConfig.API.omg + '/' +  this.orgData.guid)
                    .subscribe(
                        (val) => {
                            this.nznot.create('success', val.msg , val.msg);
                            this.initData(); // 重新查询树
                            this.router.navigate(['org']);
                            this.tabShow = false;
                        },
                        (error) => {
                            this.initData(); // 重新查询树
                            this.nznot.create('error', error.msg , error.msg);
                        });
            },
            onCancel: () => {
                console.log('失败');
            }
        });
    }



    // tab切换
    getRouter(it){
        this.selectIndex = it.index;
        this.router.navigate([this.tabs[it.index].router, this.id]);
    }


    // 批量展开方法递归
    exportTree(node) {
        for (let i = 0; i < node.length; i++) {
            if(node[i].isleaf === '否') {
                this.getData(node[i]); // 去请求该节点下一个节点
                this.exportTree(node[i].children);
            }
        }
    }



    // 树节点三个按钮方法
    clickFunc($event) {
        if($event === 'reset') {
            this.initData();
        } else if($event === 'expand') { // 全部展开方法
            if(!this.treedata[0].children[0].code) {
                this.exportTree(this.treedata[0].children);
                this.treedata.forEach( node => {
                    this.expandRecursive(node, true);
                } );
            } else {
                this.treedata.forEach( node => {
                    this.expandRecursive(node, true);
                } );
            }
        } else {
            this.tabShow = false;
        }
    }


}
*/


/*新逻辑*/
import {Component, Inject, OnInit, Optional} from '@angular/core';
import {_HttpClient} from '@delon/theme';
import {MenuItem, TreeNode} from 'primeng/api';
import {UtilityService} from '../../../service/utils.service';
import {Router} from '@angular/router';
// 公共接口名
import {appConfig} from '../../../service/common';
import {OrgModule, PageModule} from '../../../service/common.module';
import {NzModalService, NzNotificationService} from 'ng-zorro-antd';
import {ReuseTabService} from '@delon/abc';
import {CommondictServe} from '../../../service/commonDict';
import { ActivatedRoute } from '@angular/router';
import * as moment from 'moment';
import {EmpModule} from '../../../service/emp';
import {PinYinUtil} from '../../../service/pinyin.util';
import {PostModule} from '../../../service/post';
import {DA_SERVICE_TOKEN, ITokenService} from '@delon/auth';

@Component({
    selector: 'app-org',
    templateUrl: './org.component.html',
    styleUrls: ['./org.component.less']
})
export class OrgComponent implements OnInit {
    funcBhvList: any; // 后台返回的行为列表
    constructor(
        private http: _HttpClient,
        private router: Router,
        private utilityService: UtilityService,
        private modal: NzModalService,
        private nznot: NzNotificationService,
        private commondict: CommondictServe,
        private activatedRoute: ActivatedRoute, // 注入路由，接收到参数
        @Inject(DA_SERVICE_TOKEN) private tokenService: ITokenService,
        @Optional() @Inject(ReuseTabService) private reuseTabService: ReuseTabService,
    ) { }

    treedata: any[]; // tree组件数据
    treemenus: MenuItem[];
    searchTitle: string; // 树搜索框文字
    tabShow: boolean;
    id: string;
    orgName: string; // 组织机构名称
    orgVisible = false; // 弹出框默认关闭
    isEdit = false; // 默认是新增
    orgData: any; // 树节点上的数据保存
    istrue = false;  // 是否请求过
    orgItem: OrgModule = new OrgModule(); // 绑定数据
    // 枚举值
    orgDegree: any;
    area: any;
    orgType: any;
    treeResult: string; // 接受请求id值
    isroot = true; // 是否是子节点调用
    dataOrg: any; // 节点信息
    selectionType: string; // 树结构类型
    orgTitle: string;
    // 布局样式
    widthLeft = 7;
    widthRight = 17;
    isShow = true;
    loading = false;
    text = '收起'
    selectIndex = 0; // tab索引初始化
    tabId: string; // tab定位
    funcId:string;
    // 自定义卡片收缩方法
    toggleCollapsed(): void {
        this.isShow = !this.isShow;
        if(this.isShow === true){
            this.widthLeft = 7;
            this.widthRight = 17;
            this.text = '收起'
        }else{
            this.widthLeft = 2;
            this.widthRight = 22;
            this.text = '展开'
        }
    }
    // tabs标题
    tabs = [
        {
            name   : '员工',
            router : 'org/emp',
            id: 'emp'
        },
        {
            name   : '岗位',
            router : 'org/post',
            id: 'post'
        }
    ];


    ngOnInit() {
        this.initData();
        this.searchTitle = '请输入机构代码/名称';
        // 枚举值转换
        this.selectionType = 'single';
        this.reuseTabService.title = '组织机构'
        this.orgName = '组织机构';
        this.funcBhvList = this.tokenService.get().funcList;
        this.dictQueryOrgKey()
    }



    // 根据key查询业务字典
    dictQueryOrgKey() {
        this.commondict.queryDictKey(appConfig.dictKeyList.dictsdArea)
            .subscribe((val) => {
                this.area = val.result;
            })

        this.commondict.queryDictKey(appConfig.dictKeyList.orgType)
            .subscribe((val) => {
                this.orgType = val.result;
            })
        this.commondict.queryDictKey(appConfig.dictKeyList.dictOrgde)
            .subscribe((val) => {
                this.orgDegree = val.result;
            })
    }



    initData() {
        this.treedata = [ // 默认根节点
            {
                'label': '组织机构',
                'data': 'Documents Folder',
                'guid': 'null',
                'expandedIcon': 'fa fa-institution',
                'collapsedIcon': 'fa fa-institution',
                'children': [{}]
            }
        ];
        // 从服务器获取树列表
        this.utilityService.getData(appConfig.serverUrl  + appConfig.API.omgTree)
            .subscribe(
                (val) => {

                    for (let i = 0 ; i < val.result.length; i++) {
                        if (val.result[i].isleaf === '是') { // 代表是最底层，没有下级了
                            val.result[i].collapsedIcon = 'anticon anticon-usb';
                        } else {
                            val.result[i].expandedIcon = 'fa fa-institution';
                            val.result[i].collapsedIcon = 'fa fa-institution';
                            val.result[i].children = val.result[i].childrens;
                            this.iteration(val.result[i].children);
                        }

                    }
                    this.treedata[0].children = val.result;
                    this.treedata.forEach( node => {
                        this.expandRecursive(node, true);
                    });
                });
    }



    iteration(node) {
        for (let i = 0; i < node.length; i++) {
            if (node[i].childrens !== undefined && node[i].childrens.length > 0) {
                node[i].expandedIcon = 'fa fa-institution';
                node[i].collapsedIcon = 'fa fa-institution';
                node[i].children = node[i].childrens; // 重新赋值
                this.iteration(node[i].children); // 继续调用
            } else { // 字典项
                node[i].collapsedIcon = 'anticon anticon-usb';
                node[i].expandedIcon = 'anticon anticon-usb';
            }
        }
    }

    getOrgData(event) {
        if (event.guid !== 'null') {
            // 从服务器获取树列表
            this.utilityService.postData(appConfig.serverUrl  + appConfig.API.omgTree + '/' + event.guid, {})
                .subscribe(
                    (val) => {
                        for (let i = 0 ; i < val.result.children.length; i++) {
                            if (val.result.children[i].isleaf === '是') { // 代表是最底层，没有下级了
                                val.result.children[i].collapsedIcon = 'anticon anticon-usb';
                            } else {
                                // val.result.children[i].label = val.result.children[i].orgName;
                                val.result.children[i].expandedIcon = 'fa fa-institution';
                                val.result.children[i].collapsedIcon = 'fa fa-institution';
                                val.result.children[i].children =  [{}];
                            }

                        }
                        event.children = val.result.children;
                    });
        }


    }




    // 树节点搜索框的内容
    searchVal($event) {
        let jsonObj = {
            guid: 'null',
            name: $event
        }
        this.utilityService.postData(appConfig.serverUrl  + appConfig.API.orgTreeFilter, jsonObj)
            .subscribe(
                (val) => {
                    this.treedata = [ // 默认根节点
                        {
                            'label': '组织机构',
                            'data': 'Documents Folder',
                            'guid': 'null',
                            'expandedIcon': 'fa fa-institution',
                            'collapsedIcon': 'fa fa-institution',
                            'children': [{}]
                        }
                    ];
                    this.treedata[0].children = val.result.children;
                    for(let i = 0 ; i < val.result.children.length; i++) {
                        this.getOrgData(val.result.children[i]);
                    }
                    // 展开所有的
                    this.treedata.forEach( node => {
                        this.expandRecursive(node, true);
                    } );

                },
            );
    }

    // 树的方法

    // 展开节点事件
    Unfold(event) {
        this.getOrgData(event.node);
    }


    // 右击菜单传递值
    RightSelect(event) {
        console.log(event.node);
        if (event.node.guid === 'null') { // 子业务字典
            this.treemenus = [
                {label: '新建根机构', icon: 'fa fa-plus', id: appConfig.funcObject.org.addRootOrg,  command: (event) => this.addrootOrg()},
            ];
        } else {  // 字典项
            this.treemenus = [
                {label: '新增子机构', icon: 'fa fa-plus',id: appConfig.funcObject.org.addchildOrg, command: (event) => this.addchildOrg()},
                {label: '修改机构', icon: 'fa fa-edit', id: appConfig.funcObject.org.eidtOrg, command: (event) => this.editOrg()},
                {label: '删除机构', icon: 'fa fa-times', id: appConfig.funcObject.org.delOrg, command: (event) => this.delectOrg()},
            ];
        }
        delete event.node.parent;
        this.orgData = event.node; // 绑定数据
    }

    // 左击树菜单节点信息
    TreeSelect(event) {
        this.id = event.node.guid;
        this.orgName = event.node.label;
        if  (event.node.guid !== 'null') { // 只要不是跟机构就显示
            this.tabShow = true;
            this.selectIndex  = 0;
            this.tabId = 'emp'
            /* 组织机构代码逻辑初始化 */
            this.orgGuid = this.id; // 拿到父组件传过来的组织机构的guid来进行操作
            this.getData();
            // 枚举值转换
            this.conEmpfigTitle = {value: '修改', buttonCode: appConfig.funcObject.org.editorgEmp}
            this.dictQueryKey();
            this.queryOrgPost();
            // this.router.navigate(['org/emp', this.id]); // 跳转路由
        } else {
            this.selectIndex  = 0
            this.router.navigate(['org']);
            this.tabShow = false;
        }
    }
    // 拖拽方法
    dropEvent($event) {
        console.log($event)
        let originPonit = $event.dragNode.guid;
        let destinaPoint = $event.dropNode.guid;
        // 如果拖拽的目的地菜单是叶子菜单，拖拽目标目的地设为它的父菜单
        if ($event.dropNode.isleaf === 'Y') {
            destinaPoint = $event.dropNode.guidParents;
        }
        let  playOrder = $event.dragNode.displayOrder;
        let moveDate = {
            'targetGuid': destinaPoint,
            'moveGuid': originPonit,
            'order': 0
        };
        this.utilityService.postData(appConfig.serverUrl  + appConfig.API.queryMoveOrgLists , moveDate )
            .subscribe(
                (val) => {
                    this.initData()
                    this.nznot.create('success', '拖拽成功' , '拖拽成功');
                },
                (err) => {
                    // this.getOrgData($event.dropNode); // 查询目标下的子集
                    this.initData()
                    this.nznot.create('error', err.msg , err.msg);
                }
            );
    }




    // 展开所有树节点的方法
    private expandRecursive(node:TreeNode, isExpand:boolean){
        node.expanded = isExpand;
        if(node.children){
            node.children.forEach( childNode => {
                this.expandRecursive(childNode, isExpand);
            } );
        }
    }

    // 新增跟组织机构
    addrootOrg() {
        this.isroot = true;
        this.isEdit = false;
        this.orgTitle = '新增组织机构'
        this.orgItem = new OrgModule(); // 清空
        this.orgVisible = true;
    }
    // 新建子组织机构
    addchildOrg() {
        this.orgItem = new OrgModule(); // 清空
        this.orgVisible = true;
        this.orgTitle = '新增组织机构'
        this.isEdit = false;
        this.isroot = false;
    }


    // 枚举值转换
    // 机构等级转换
    degreeOrg(event) {
        if (event.orgDegree === '总行') {
            event.orgDegree = 'BS';
        } else if (event.orgDegree === '分行') {
            event.orgDegree = 'YF';
        } else if (event.orgDegree === '海外') {
            event.orgDegree = 'HW';
        } else if (event.orgDegree === '区域分行') {
            event.orgDegree = 'QY';
        } else if (event.orgType === '网点') {
            event.orgDegree = 'CN';
        }
    }
    // 机构类型转换
    typeOrg(event) {
        if (event.orgType === '总公司') {
            event.orgType = '10';
        } else if (event.orgType === '总部部门') {
            event.orgType = '11';
        } else if (event.orgType === '分公司') {
            event.orgType = '20';
        } else if (event.orgType === '分公司部门') {
            event.orgType = '21';
        } else if (event.orgType === '营业网点') {
            event.orgType = '90';
        }
    }
    //  地区
    arearOrg(event) {
        if (event.area === '北京') {
            event.area = '010';
        } else if (event.area === '上海') {
            event.area = '021';
        }
    }
    // 是否
    isleaf(event) {
        if (event.isleaf === '是') {
            event.isleaf = 'Y';
        } else if (event.isleaf === '否') {
            event.isleaf = 'N';
        }
    }
    // 机构状态
    orgStatus(event) {
        if (event.orgStatus === '正常') {
            event.orgStatus = 'running';
        } else if (event.orgStatus === '注销') {
            event.orgStatus = 'cancel';
        } else if (event.orgStatus === '停用') {
            event.orgStatus = 'stop';
        }
    }

    // 保存接口
    Orgsave() {
        const jsonOption = this.orgItem;
        if (!this.isEdit) {
            if (this.isroot) { // 新增跟机构
                this.utilityService.postData(appConfig.serverUrl  + appConfig.API.addRoot, jsonOption)
                    .subscribe(
                        (val) => {
                            this.initData()
                            this.nznot.create('success', val.msg , val.msg);
                        },
                        (error) => {
                            this.nznot.create('error', error.msg , error.msg);
                        }
                    );
            } else {
                jsonOption.guidParents = this.orgData.guid; // 绑定父机构的guid
                this.utilityService.postData(appConfig.serverUrl  + appConfig.API.addChild, jsonOption)
                    .subscribe(
                        (val) => {
                            this.initData()
                            this.nznot.create('success', val.msg , val.msg);
                        },
                        (error) => {
                            this.nznot.create('error', error.msg , error.msg);
                        }
                    );
            }
        } else {
            this.degreeOrg(jsonOption)
            this.typeOrg(jsonOption)
            this.arearOrg(jsonOption)
            this.isleaf(jsonOption)
            this.orgStatus(jsonOption)
            console.log(jsonOption)
            this.utilityService.putData(appConfig.serverUrl  + appConfig.API.omg, jsonOption)
                .subscribe(
                    (val) => {
                        this.initData()
                        this.nznot.create('success', val.msg , val.msg);
                    },
                    (error) => {
                        this.nznot.create('error', error.msg , error.msg);
                    }
                );
        }

        this.orgVisible = false;
    }


    // 修改组织机构
    editOrg() {
        this.isEdit = true; // 是修改
        this.orgVisible = true;
        this.orgTitle = '修改组织机构'
        // 查询修改的数据信息
        this.utilityService.getData(appConfig.serverUrl + appConfig.API.omg + '/' +  this.orgData.guid)
            .subscribe(
                (val) => {
                    this.dataOrg = val.result;
                    this.degreeOrg(val.result)
                    this.typeOrg(val.result)
                    this.arearOrg(val.result)
                    this.orgItem = val.result; // 渲染数据
                });

    }


    // 删除组织机构
    delectOrg() {
        this.modal.open({
            title: '是否删除',
            content: '您确认要删除该机构吗? 删除该工作组下所有子机构都会被一并删除',
            okText: '确定',
            cancelText: '取消',
            onOk: () => {
                this.utilityService.deleatData(appConfig.serverUrl + appConfig.API.omg + '/' +  this.orgData.guid)
                    .subscribe(
                        (val) => {
                            this.nznot.create('success', val.msg , val.msg);
                            this.initData(); // 重新查询树
                            this.router.navigate(['org']);
                            this.tabShow = false;
                        },
                        (error) => {
                            this.initData(); // 重新查询树
                            this.nznot.create('error', error.msg , error.msg);
                        });
            },
            onCancel: () => {
                console.log('失败');
            }
        });
    }



    // tab切换
    getRouter(it){
        this.selectIndex = it.index;
        if(it.index === 0) {
            this.tabId = 'emp'
            /*初始化代码员工*/
            this.orgGuid = this.id; // 拿到父组件传过来的组织机构的guid来进行操作
            this.getData();
            // 枚举值转换
            this.conEmpfigTitle = {value: '修改', buttonCode: appConfig.funcObject.org.editorgEmp}
            this.dictQueryKey();
            this.queryOrgPost();

        } else {
            this.tabId = 'post'
            this.orgGuid = this.id; // 拿到父组件传过来的组织机构的guid来进行操作
            // 枚举值转换
            this.postStatus = appConfig.Enumeration.postStatus;
            this.positionType = appConfig.Enumeration.postType;
            this.data = []; // 切换的时候 默认为空
            this.getPostData(); // 只会触发一次，但是ngchanges并不会触发咋办
            this.queryAppId();
            // 没有修改机构岗位的行为代码，先和员工的一样，明天加了在改
            this.configTitle =  {value: '修改', buttonCode: appConfig.funcObject.org.editorgEmp}
        }
        // this.router.navigate([this.tabs[it.index].router, this.id]);
    }


    // 批量展开方法递归
    exportTree(node) {
        for (let i = 0; i < node.length; i++) {
            if(node[i].isleaf === '否') {
                this.getOrgData(node[i]); // 去请求该节点下一个节点
                this.exportTree(node[i].children);
            }
        }
    }



    // 树节点三个按钮方法
    clickFunc($event) {
        if($event === 'reset') {
            this.initData();
        } else if($event === 'expand') { // 全部展开方法
            if(!this.treedata[0].children[0].code) {
                this.exportTree(this.treedata[0].children);
                this.treedata.forEach( node => {
                    this.expandRecursive(node, true);
                } );
            } else {
                this.treedata.forEach( node => {
                    this.expandRecursive(node, true);
                } );
            }
        } else {
            this.tabShow = false;
        }
    }














    /*------------------------------机构下员工代码逻辑-----------------------------*/
    emp: EmpModule = new EmpModule();
    empAdd: EmpModule = new EmpModule();

    ifshow = true;
    gender: any;
    page: any;
    // 员工状态
    empType: any;
    // 直接主管
    supervisor: any;

    operData: any; // 操作员

    empGuid: string; // 员工guid
    isSlash = false; // 是否打开alert验证框

    // 证件类型
    paperType: any;
    obtitle: string; // 窗口状态
    expandForm = false;
    // 表头按钮
    Empbuttons = [
        {key: 'add', value: '新增员工',  buttonCode: appConfig.funcObject.org.addorgEmp}
    ]
    // 操作员类型
    radioValue = [
        {text: '新建默认操作员', value: false, key: 'creat'},
        {text: '选择已存在的', value: false, key: 'extant'},
        {text: '暂不选择操作员', value: false, key: 'noselect'}
    ];
    selectedEmpRows: any; // 选中的总数
    // 弹出框默认关闭
    modalVisible = false;
    onboarding = false;
    departure = false; // 离职弹窗
    isEmpEdit = false; // 默认是新增，不是修改
    Emptotal: number;
    emppost: any; // 基本岗位
    empTitle: string; // 员工弹窗标题

    Empdata: any[] = []; // 表格数据
    headerEmpData = [  // 配置表头内容
        {value: '员工姓名', key: 'empName', isclick: false},
        {value: '操作员', key: 'userId', isclick: false},
        {value: '基本岗位', key: 'positionName', isclick: false},
        {value: '性别', key: 'gender', isclick: false},
        {value: '员工状态', key: 'empstatus', isclick: false},
        {value: '电话号码', key: 'mobileno', isclick: false},
        {value: '入职日期', key: 'indate', isclick: false},
    ];

    pageIndex=1; // 当前页数
    moreEmpData = {
        morebutton: true,
        buttons: [
            {key: 'Overview', value: '查看概况'}
        ]
    };

    orgGuid: string;
    conEmpfigTitle: any;



    queryAppId() {
        this.utilityService.getData(appConfig.serverUrl  + appConfig.API.omg + '/' + this.orgGuid)
            .subscribe(
                (val) => {
                    this.orgName = val.result.orgName;
                }
            );
    }
    dictQueryKey() {
        this.commondict.queryDictKey(appConfig.dictKeyList.gender)
            .subscribe((val) => {
                this.gender = val.result;
            })

        this.commondict.queryDictKey(appConfig.dictKeyList.paperType)
            .subscribe((val) => {
                this.paperType = val.result;
            })

        this.commondict.queryDictKey(appConfig.dictKeyList.empStatus)
            .subscribe((val) => {
                this.empType = val.result;
            })
    }


    getData(option?) { // 初始化请求后台数据

        if (option) {
            this.pageIndex = 1;
        }
        this.page = {
            condition: this.objJson, // 搜索内容
            page: {
                current: this.pageIndex,
                size: this.emp.size,
            }
        };

        // 查询组织机构下所有员工
        this.utilityService.postData(appConfig.serverUrl + appConfig.API.queryorgList + '/' + this.orgGuid, this.page)
            .subscribe(
                (val) => {
                    console.log(val.result)
                    for (let i = 0; i < val.result.records.length; i++) {
                        if (val.result.records[i].empstatus === '在招') {
                            val.result.records[i].buttonData = [{key: 'onJob', value: '入职', buttonCode: appConfig.funcObject.org.onJob}, {key: 'dels', value: '删除', buttonCode: appConfig.funcObject.org.delorgEmp}];
                        } else if (val.result.records[i].empstatus === '在职') {
                            val.result.records[i].buttonData = [{key: 'offjob', value: '离职', buttonCode: appConfig.funcObject.org.offJob}, {key: 'eitonJob', value: '修改入职', buttonCode: appConfig.funcObject.org.onJobEdit}];
                        } else {
                            val.result.records[i].buttonData = [ {key: 'dels', value: '删除', buttonCode: appConfig.funcObject.org.delorgEmp}];
                        }
                        if(val.result.records[i].indate) {
                            val.result.records[i].indate =  moment(val.result.records[i].indate).format('YYYY-MM-DD');
                        }
                    }
                    this.Empdata = val.result.records;
                    this.Emptotal = val.result.total;
                });
    }


    queryOrgEmp(empGuid?) {
        if(empGuid) {
            let objJson = {
                guid: empGuid,
                guidOrg: this.orgGuid
            }
            this.utilityService.postData(appConfig.serverUrl + appConfig.API.getEmployeeMajors, objJson)
                .subscribe(
                    (val) => {
                        this.supervisor = val.result;
                    });
        } else {
            let objJson = {
                guidOrg: this.orgGuid
            }
            this.utilityService.postData(appConfig.serverUrl + appConfig.API.getEmployeeMajors, objJson)
                .subscribe(
                    (val) => {
                        this.supervisor = val.result;
                    });
        }
    }
    // 枚举值转换
    empStatus(event) {
        if (event.empstatus === '在招') {
            event.empstatus = 'offer';
        } else if (event.empstatus === '在职') {
            event.empstatus = 'onjob';
        } else if (event.empstatus === '离职') {
            event.empstatus = 'offjob';
        }
    }

    empgender(event) {
        if (event.gender === '男') {
            event.gender = 'M';
        } else if (event.gender === '女') {
            event.gender = 'F';
        } else if (event.gender === '未知') {
            event.fromType = 'U';
        }
    }

    typePaper(event) {
        if (event.paperType === '身份证') {
            event.paperType = '01';
        } else if (event.paperType === '军官证') {
            event.paperType = '03';
        } else if (event.paperType === '户口薄') {
            event.paperType = '02';
        } else if (event.paperType === '学生证') {
            event.paperType = '04';
        } else if (event.paperType === '护照') {
            event.paperType = '05';
        } else if (event.paperType === '其它') {
            event.paperType = '06';
        }
    }

    // 想一下，能否把这三个方法封装到一个ts里面，引入即可，不然每次都写着三个方法不太现实。
    // 列表组件传过来的内容
    addEmpHandler(event) {
        this.empAdd = new EmpModule(); // 重新清空赋值
        this.ifshow = false; // 默认是基础信息
        this.isSlash = false;
        // this.empCodeexit();  // 验证员工代码是否重复
        if (event === 'add') {
            this.isEmpEdit = false;
            this.empAdd.radioValue = 'creat';
            this.queryOrgPost();
            this.empTitle = '新增员工'
            this.queryOrgEmp(); // 调用查询
            this.modalVisible = true;  // 此时点击了列表组件的新增，打开模态框
        } else { // 代表修改，把修改的内容传递进去，重新渲染
            this.isEmpEdit = true;  // 修改
            this.empTitle = '修改员工';
            this.typePaper(event)
            this.empgender(event);
            this.queryOrgEmp(event.guid)
            this.empAdd =  event;
            this.queryOrgPost();
            this.modalVisible = true;  // 此时点击了列表组件的新增，打开模态框

        }
    }

    // 列表传入的翻页数据
    monitorEmpHandler(event) {
        this.pageIndex = event;
        this.getData();

    }


    // 批量刪除
    deleteEmpBatch(event) {
        this.modal.open({
            title: '是否删除',
            content: '您是否确认删除这' + event.length + '条员工吗?',
            okText: '确定',
            cancelText: '取消',
            onOk: () => {
                let delarray = [];
                for( let i = 0; i < event.length; i++) {
                    delarray.push(event[i].guid);
                }
                let deletaObj = {
                    deleteList: delarray,
                    guid: this.orgGuid
                }
                this.utilityService.postData(appConfig.serverUrl + appConfig.API.empDelete, deletaObj )
                    .subscribe(
                        (val) => {
                            this.getData();
                            this.selectedEmpRows = []
                            this.nznot.create('success', val.msg , val.msg);
                        },
                        (error) => {
                            this.nznot.create('error', error.msg , error.msg);
                        });
            },
            onCancel: () => {
                this.selectedEmpRows = []
                this.getData()
            }
        });
    }



    // 列表按钮方法
    buttonEmpDataHandler(event) {
        if (event.value === 'Overview') {
            console.log('查看概况');
        }

        // 如果有操作员 则编辑操作员
        if (event.value === 'operator') {
            console.log('员工操作员修改');
        }
    }



    // 右侧按钮方法
    buttonEmpEvent(e) {
        console.log(e)
        if (e.names) {
            if (e.names.key === 'onJob') {
                this.empAdd = new EmpModule();
                this.onboarding = true; // 打开入职弹出框
                this.empGuid = e.guid;
                this.empAdd.radioValue = 'creat'; // 默认是新建
                this.obtitle = '员工入职';

            } else if (e.names.key === 'offjob') {
                this.empAdd = new EmpModule();
                this.departure = true; // 打开离职弹出框
                this.empGuid = e.guid;
            }   else if (e.names.key === 'eitonJob') {
                this.empAdd = new EmpModule();
                this.onboarding = true; // 打开弹出框
                this.empGuid = e.guid;
                this.obtitle = '修改入职';
            } else if (e.names.key === 'dels') {
                this.modal.open({
                    title: '是否删除',
                    content: '您是否确认删除所选员工?',
                    okText: '确定',
                    cancelText: '取消',
                    onOk: () => {
                        let deletaObj = {
                            deleteList: [e.guid],
                            guid: this.orgGuid
                        }
                        this.utilityService.postData(appConfig.serverUrl + appConfig.API.empDelete, deletaObj )
                            .subscribe(
                                (val) => {
                                    this.nznot.create('success', val.msg , val.msg);
                                    if ( !(( this.Emptotal - 1) % 10)) {
                                        // if ( !(( this.Emptotal - this.acfundata.length) % 10)) { // 支持批量删除的方法
                                        this.emp.pi -- ;
                                        this.getData();
                                    }
                                    this.getData();
                                });
                    },
                    onCancel: () => {
                        console.log('取消成功');
                    }
                });
            }
        }
    }


    selectedEmpRow(event) { // 选中方法，折叠层按钮显示方法
        if (event.indeterminate) {
            if (event.selectedRows[0].empType === '在职') {
                this.moreEmpData.buttons = [
                    {key: 'Departure' , value: '离职'},
                    {key: 'Overview' , value: '查看概况'},
                    {key: 'orgsettings' , value: '组织机构设置'},
                    {key: 'operator' , value: '操作员修改'},
                ];
            }
            if (event.selectedRows[0].empType === '在招') {
                this.moreEmpData.buttons = [
                    {key: 'Onboarding' , value: '入职'},
                    {key: 'Overview' , value: '查看概况'},
                    {key: 'orgsettings' , value: '组织机构设置'},
                    {key: 'operator' , value: '操作员修改'},
                ];
            }
        }

    }




    objJson = {};
    // 搜索框
    empsearch() {
        this.objJson = this.emp;
        this.getData(this.emp);
    }


    Empreset() {
        this.emp = new EmpModule();
        this.objJson = {};
        this.getData();
    }


    // 弹出框保存组件
    Empsave() {
        const jsonOption = this.empAdd;
        jsonOption.guidOrg = this.orgGuid;
        if (!this.isEmpEdit) { // 新增数据
            this.utilityService.postData(appConfig.serverUrl  + appConfig.API.emporgAdd, jsonOption)
                .subscribe(
                    (val) => {
                        this.nznot.create('success', val.msg , val.msg);
                        this.getData();
                    },
                    (error) => {
                        this.getData();
                        this.nznot.create('error', error.msg , error.msg);
                    }
                );
        } else {
            this.empStatus(jsonOption);
            this.utilityService.putData(appConfig.serverUrl  + appConfig.API.emporgAdd, jsonOption)
                .subscribe(
                    (val) => {
                        this.nznot.create('success', val.msg , val.msg);
                        this.getData();
                    },
                    (error) => {
                        this.getData();
                        this.nznot.create('error', error.msg , error.msg);
                    }
                );
        }

        this.modalVisible = false;
    }

    // 取消方法
    cancel() {
        this. modalVisible = false;
        this.getData();
    }

    // 点击入职的方法
    select(i) {
        if (i.key === 'noselect') { // 如果是不选择，那么就为空
            this.empAdd.userId = '';
        }
    }

    checkSelect(e) {
        if ( e === 'extant') {
            this.utilityService.getData(appConfig.serverUrl + appConfig.API.queryOperatorForEmployee)
                .subscribe(
                    (val) => {
                        if(val.result.length === 0) {
                            this.isSlash = true;
                        }
                        this.operData = val.result;
                    }
                );
        }
    }

    // 入职弹出框保存按钮
    onboardingsave() {
        if (this.obtitle === '员工入职') {
            const jsonObj = this.empAdd;
            jsonObj.operatorName = this.empAdd.userId;
            if (jsonObj.userId) {
                jsonObj.userId  = PinYinUtil.chineseTopinYin(jsonObj.userId).toLowerCase();
            }
            if (jsonObj.indate) {
                jsonObj.indate = moment(jsonObj.indate).format('YYYY-MM-DD');
            } else { // 不存在，默认给一个当前时间
                jsonObj.indate = moment(new Date()).format('YYYY-MM-DD');
            }
            jsonObj.guid = this.empGuid;
            if (this.empAdd.radioValue === 'creat') {
                // 先调用新增操作员接口
                this.utilityService.postData(appConfig.serverUrl + appConfig.API.acOperatorsAdd, jsonObj)
                    .subscribe(
                        (val) => {
                            if (val.code  === '200') {
                                // 调用入职接口
                                this.utilityService.putData(appConfig.serverUrl  + appConfig.API.onJob, jsonObj)
                                    .subscribe(
                                        (src) => {
                                            this.nznot.create('success', src.msg , src.msg);
                                            this.onboarding = false; // 关闭入职弹出框
                                            this.getData();
                                        },
                                        (error) => {
                                            this.nznot.create('error', error.msg , error.msg);
                                        }
                                    );
                            } else {
                                this.nznot.create('error', val.msg , val.msg);
                            }

                            this.getData();
                        },

                    );
            } else {
                this.utilityService.putData(appConfig.serverUrl  + appConfig.API.onJob, jsonObj)
                    .subscribe(
                        (src) => {
                            this.nznot.create('success', src.msg , src.msg);
                            this.onboarding = false; // 关闭入职弹出框
                            this.getData();
                        }, (error) => {
                            this.nznot.create('error', error.msg , error.msg);
                        }
                    );
            }
        } else {
            const jsonObj = this.empAdd;
            jsonObj.operatorName = this.empAdd.userId;
            if (jsonObj.userId) {
                jsonObj.userId  = PinYinUtil.chineseTopinYin(jsonObj.userId).toLowerCase();
            }
            if (jsonObj.indate) {
                jsonObj.indate = moment(jsonObj.indate).format('YYYY-MM-DD');
            }else { // 不存在，默认给一个当前时间
                jsonObj.indate = moment(new Date()).format('YYYY-MM-DD');
            }
            jsonObj.guid = this.empGuid;
            if (this.empAdd.radioValue === 'creat') {
                // 先调用洗澡操作员接口
                this.utilityService.postData(appConfig.serverUrl + appConfig.API.acOperatorsAdd, jsonObj)
                    .subscribe(
                        (val) => {
                            if (val.code  === '200') {
                                // 调用入职接口
                                this.utilityService.putData(appConfig.serverUrl  + appConfig.API.changeOnJob, jsonObj)
                                    .subscribe(
                                        (src) => {
                                            this.nznot.create('success', src.msg , src.msg);
                                            this.onboarding = false; // 关闭入职弹出框
                                            this.getData();
                                        },
                                    );
                            } else {
                                this.nznot.create('error', val.msg , val.msg);
                            }

                            this.getData();
                        },

                    );
            } else {
                this.utilityService.putData(appConfig.serverUrl  + appConfig.API.changeOnJob, jsonObj)
                    .subscribe(
                        (src) => {
                            console.log(src)
                            this.nznot.create('success', src.msg , src.msg);
                            this.onboarding = false; // 关闭入职弹出框
                            this.getData();
                        },
                        (error) => {
                            this.nznot.create('error', error.msg , error.msg);
                        }
                    );
            }


        }

    }

    // 点击离职方法
    ondeparturesave() {
        const jsonObj = this.empAdd;
        if (jsonObj.outdate) {
            jsonObj.outdate = moment(jsonObj.outdate).format('YYYY-MM-DD');
        } else { // 不存在，默认给一个离职日期
            jsonObj.outdate = moment(new Date()).format('YYYY-MM-DD');
        }
        jsonObj.guid = this.empGuid;
        this.utilityService.putData(appConfig.serverUrl  + appConfig.API.outJob, jsonObj)
            .subscribe(
                (val) => {
                    this.nznot.create('success', val.msg , val.msg);
                    this.departure = false; // 关闭离职弹出框
                    this.getData();
                },
                (error) => {
                    this.nznot.create('error', error.msg , error.msg);
                }
            );
    }


    // 完善其他信息
    basicinfo() {
        this.ifshow = !this.ifshow;
    }

    // 返回上一步
    addBack() {
        console.log(1)
        this.ifshow = !this.ifshow;
    }
    empCodeexit(item) {
        this.utilityService.getData(appConfig.serverUrl  + appConfig.API.queryEmpCode + '/' + item)
            .subscribe(
                (sus) => {
                    if (sus.result) {
                        this.isSlash = true; // 重复
                    } else {
                        this.isSlash = false; // 不重复
                    }
                },
                (error) => {
                    this.nznot.create('error', error.msg , error.msg);
                }
            );
    }

 /*------------------------------------------------机构下岗位----------------------------------------------*/


    post: PostModule = new PostModule();
    postAdd: PostModule = new PostModule();
    // 岗位类型
    positionType: any;
    // 状态
    postStatus: any;
    postPage: any;
    postAddEdit: string; // 新增/修改 崗位
    postTitle: string;
    isappcodeSlash = false; // 验证服务

    // blDuct 所属职务
    blDuct= [
        {key: 'practice' , value: '行长'},
        {key: 'primary' , value: '业务经理'},
        {key: 'intermediate' , value: '理财经理'},
        {key: 'Senior' , value: '营销经理'},
        {key: 'scientist' , value: '大厅经理'},
        {key: 'management' , value: '会计'},
        {key: 'Director' , value: '总管'},
    ];

    total: number;
    postGuid: string;
    pages: PageModule = new PageModule(); // 分页内容
    // 岗位员工
    PostmodalVisible = false;
    empdistribution = false;
    modelSelect: boolean; // 弹出框内容选择
    // switch控制
    parentPost = false;
    startDate = false;
    endDate = false;
    PagepageIndex=1; // 当前页数
    selectedRows: any; // 提交总数
    isPostEdit = false; // 默认是新增 不是修改
    data: any[] = []; // 表格数据
    headerData = [  // 配置表头内容
        {value: '岗位名称' , key: 'positionName', isclick: false},
        {value: '崗位代碼' , key: 'positionCode', isclick: false},
        {value: '上级岗位' , key: 'parentName', isclick: false},
        {value: '岗位状态' , key: 'positionStatus', isclick: false},
        {value: '在岗员工数' , key: 'employeeCount', isclick: true},
        {value: '岗位有效日期' , key: 'startDate', isclick: false},
        {value: '岗位失效日期' , key: 'endDate', isclick: false},
    ];


    moreData = {
        morebutton: true,
        buttons: [
            {key: 'Overview' , value: '查看概况'},
        ]
    };

    copy: any;

    Parentsguid: any;
    configTitle: any;
    appConfit: string;
    // 表头按钮
    buttons = [
        {key: 'add', value: '新增岗位',  buttonCode: appConfig.funcObject.org.addorgRootpost}
    ]




    getPostData(option?) { // 初始化请求后台数据
        if (option) {
            this.PagepageIndex = 1;
        }
        this.postPage = {
            condition: this.objJson, // 搜索内容
            page: {
                current: this.PagepageIndex,
                size: this.post.size,
            }
        };
        // 查询组织机构下所有岗位
        this.utilityService.postData(appConfig.serverUrl + appConfig.API.posttreeList + '/' + this.orgGuid ,  this.postPage)
            .subscribe(
                (val) => {
                    // 没有在岗员工，模拟一下
                    for ( let i = 0; i < val.result.records.length; i++) {
                        if ( val.result.records[i].positionStatus === '正常') {
                            val.result.records[i].buttonData = [ {key: 'logOut', value: '注销', if: false, buttonCode: appConfig.funcObject.org.cancelpost}, {key: 'app', value: '应用权限', if: false, buttonCode: appConfig.funcObject.org.orgPostApp}];
                        } else {
                            val.result.records[i].buttonData = [ {key: 'start', value: '启用', if: false, buttonCode: appConfig.funcObject.org.postOpen} , {key: 'dels', value: '删除', if: false, buttonCode: appConfig.funcObject.org.delorgchildpost}];
                        }
                        if (val.result.records[i].startDate ) {
                            val.result.records[i].startDate = moment(val.result.records[i].startDate).format('YYYY-MM-DD');
                        }
                        if (val.result.records[i].endDate) {
                            val.result.records[i].endDate = moment(val.result.records[i].endDate).format('YYYY-MM-DD');
                        }
                    }
                    this.data = val.result.records;
                    this.total = val.result.total;
                });

    }

    // 枚举值转换
    // 岗位类型转换
    postType(event) {
        if (event.positionType === '机构岗位') {
            event.positionType = '01';
        } else if (event.positionType === '工作组岗位') {
            event.positionType = '02';
        }
    }
    // 岗位状态转换
    Statuspost(event) {
        if (event.positionStatus === '正常') {
            event.positionStatus = 'running';
        } else if (event.positionStatus === '注销') {
            event.positionStatus = 'cancel';
        }
    }


    /* 获取机构下可分配岗位*/
    queryOrgPost(postGuid?) {
        if(postGuid) {
            let objJson = {
                guid: postGuid,
                orgId: this.orgGuid
            }
            this.utilityService.postData(appConfig.serverUrl + appConfig.API.listsByOrgId, objJson)
                .subscribe(
                    (val) => {
                        this.Parentsguid = val.result;
                    });
        } else {
            let objJson = {
                orgId: this.orgGuid
            }
            this.utilityService.postData(appConfig.serverUrl + appConfig.API.listsByOrgId, objJson)
                .subscribe(
                    (val) => {
                        this.Parentsguid = val.result;
                    });
        }
    }



    // 想一下，能否把这三个方法封装到一个ts里面，引入即可，不然每次都写着三个方法不太现实。
    // 列表组件传过来的内容
    addHandler(event) {
        this.ifshow = false; // 默认是基础信息
        this.isappcodeSlash = false;
        this.postAdd = new PostModule(); // 重新清空赋值
        if (event === 'add') {
            this.postAdd.positionStatus = 'running';
            this.PostmodalVisible = true;  // 此时点击了列表组件的新增，打开模态框
            this.postAddEdit = '新增岗位'
            this.startDate = false; // 默认false
            this.parentPost = false; // 默认false
            this.endDate = false; // 默认false
            this.queryOrgPost();
            this.isPostEdit = false;
        } else { // 代表修改，把修改的内容传递进去，重新渲染
            if (event.guidParents !== '') {
                this.parentPost = true;
            }
            if (event.startDate) {
                this.startDate = true;
            }
            if (event.endDate) {
                this.endDate = true;
            }
            this.Parentsguid = [];
            this.postAddEdit = '修改岗位'
            this.queryOrgPost(event.guid);
            this.Statuspost(event)
            this.postType(event)
            this.postAdd = event;
            this.PostmodalVisible = true;  // 此时点击了列表组件的新增，打开模态框
            this.isPostEdit = true;
        }
    }



    // 验证是否重复
    AppCodeexit(postAddEdit, postCode) {
        if(postAddEdit === '新增岗位') {
            let objJson = {
                guid: '',
                positionCode: postCode,
                guidMapping: this.orgGuid
            }
            this.utilityService.postData(appConfig.serverUrl  + appConfig.API.existAddPositionCode , objJson)
                .subscribe(
                    (sus) => {
                        if (sus.result) {
                            this.isappcodeSlash = true; // 重复
                        } else {
                            this.isappcodeSlash = false; // 不重复
                        }
                    },
                    (error) => {
                        this.nznot.create('error', error.msg , error.msg);
                    }
                );
        } else {
            let objJson = {
                guid: this.postAdd.guid,
                positionCode: postCode,
                guidMapping: this.orgGuid
            }
            this.utilityService.postData(appConfig.serverUrl  + appConfig.API.existUpdatePositionCode , objJson)
                .subscribe(
                    (sus) => {
                        if (sus.result) {
                            this.isappcodeSlash = true; // 重复
                        } else {
                            this.isappcodeSlash = false; // 不重复
                        }
                    },
                    (error) => {
                        this.nznot.create('error', error.msg , error.msg);
                    }
                );
        }
    }

    buttonEvent(event) {
        console.log(event)
        if (event.names) {
            if (event.names.key === 'logOut') {
                this.utilityService.putData(appConfig.serverUrl  + appConfig.API.cancel + '/' + event.guid )
                    .subscribe(
                        (val) => {
                            console.log(val)
                            this.nznot.create('success', val.msg , val.msg);
                            this.getPostData();
                        },
                        (error) => {
                            this.nznot.create('error', error.msg , error.msg);
                        }
                    );
            }
            if (event.names.key === 'app') {
                this.showAdd = true; // 没有新增
                this.postName = event.postName;
                this.appConfit = '';
                this.postGuid = event.guid;
                this.modelSelect = false; // 打开弹出框
                this.funcId = appConfig.funcObject.org.orgPostApp;
                this.empdistribution = true; // 弹出在岗员工数代码
                this.postTitle = '岗位' + event.positionName + '可以使用的应用系统'
                this.getPostApp(); // 查询不在岗位下的应用
                this.getPostApplist(); // 查询岗位下应用
            }

            if (event.names.key === 'start') {
                this.utilityService.putData(appConfig.serverUrl  + appConfig.API.running + '/' + event.guid )
                    .subscribe(
                        (val) => {
                            this.nznot.create('success', val.msg , val.msg);
                            this.getPostData();
                        },
                        (error) => {
                            this.nznot.create('error', error.msg , error.msg);
                        }
                    );
            }
            if (event.names.key === 'dels') {
                this.modal.open({
                    title: '是否删除',
                    content: '您是否确认删除所选岗位?',
                    okText: '确定',
                    cancelText: '取消',
                    onOk: () => {
                        this.utilityService.deleatData(appConfig.serverUrl  + appConfig.API.postDel + '/' + event.guid )
                            .subscribe(
                                (val) => {
                                    console.log(val)
                                    this.nznot.create('success', val.msg , val.msg);
                                    this.getPostData();
                                },
                                (error) => {
                                    this.nznot.create('error', error.msg , error.msg);
                                }
                            );
                    },
                    onCancel: () => {
                    }
                });
            }
        } else {

        }
    }


    // 列表传入的翻页数据
    monitorHandler(event) {
        this.PagepageIndex = event;
        this.getPostData();
    }

    // 接受子组件删除的数据 单条还是多条
    deleatData(event) {
        this.modal.open({
            title: '是否删除',
            content: '您是否确认删除所选岗位?',
            okText: '确定',
            cancelText: '取消',
            onOk: () => {
                this.utilityService.deleatData(appConfig.serverUrl + appConfig.API.postDel + '/' + event[0].guid)
                    .subscribe(
                        (val) => {
                            this.nznot.create('success', val.msg , val.msg);
                            if ( !(( this.total - 1) % 10)) {
                                // if ( !(( this.total - this.acfundata.length) % 10)) { // 支持批量删除的方法
                                this.post.pi -- ;
                                this.getPostData();
                            }
                            this.getPostData();
                        }, (error) => {
                            this.nznot.create('error', error.msg , error.msg);
                        });

            },
            onCancel: () => {
                console.log('取消成功');
            }
        });
    }


    // 批量删除
    deleteBatch(event) {
        this.modal.open({
            title: '是否删除',
            content: '您是否确认删除这' + event.length + '条岗位吗?',
            okText: '确定',
            cancelText: '取消',
            onOk: () => {
                let delarray = [];
                for( let i = 0; i < event.length; i++) {
                    delarray.push(event[i].guid);
                }
                let deletaObj = {
                    deleteList: delarray,
                    guid: this.orgGuid
                }
                this.utilityService.postData(appConfig.serverUrl + appConfig.API.postDelte, deletaObj )
                    .subscribe(
                        (val) => {
                            this.getPostData();
                            this.selectedRows = []
                            this.nznot.create('success', val.msg , val.msg);
                        },
                        (error) => {
                            this.nznot.create('error', error.msg , error.msg);
                        });
            },
            onCancel: () => {
                this.getPostData();
                this.selectedRows = [];
            }
        });
    }


    // 列表按钮方法
    buttonDataHandler(event) {
        console.log(event); // 根据event.value来判断不同的请求，来获取结果和方法或者进行路由的跳转
        if (event.value ===  'Authority') {
            console.log(event.key);
        }

        if (event.value ===  'Overview') {
            console.log(event.key);
        }

    }


    selectedRow(event) { // 选中方法，折叠层按钮显示方法

    }

    // 处理行为代码，跳转、弹出框、其他交互
    isActive(event) {
        this.empdistribution = true; // 弹出在岗员工数代码
        this.modelSelect = true;
        this.postTitle = '岗位' + event.parentName + '下的员工'
        this.postGuid = event.guid;
        this.getEmpData(event.guid); // 查询可以添加的员工
        this.getEmpList(event.guid); // 查询已经存在的员工
    }

    // switch开关
    switchchange(item, status) {
        if (item === 'parentPost') {
            this.parentPost = status;
        }
        if (item === 'startDate') {
            this.startDate = status;
        }
        if (item === 'endDate') {
            this.endDate = status;
        }
    }


    // 搜索框
    search() {
        this.objJson = this.post;
        this.getPostData(this.post);
    }


    reset() {
        this.post = new PostModule();
        this.objJson  = {};
        this.getPostData()
    }

    canel() {
        this.PostmodalVisible = false;
        this.getPostData();
    }

    // 弹出框保存组件
    save() {
        const jsonOption = this.postAdd;
        jsonOption.guidMapping = this.orgGuid;
        jsonOption.isLeaf = 'Y' ;
        jsonOption.subCount = 0 ;
        jsonOption.positionType = '01'; // 默认是机构岗位
        if (!this.isPostEdit) { // 新增数据
            if (this.startDate && jsonOption.startDate === undefined) { // 如果是true 那么就默认当天
                jsonOption.startDate =  moment(new Date()).format('YYYY-MM-DD'); // 默认当前时间
            }
            if (this.endDate && jsonOption.endDate === undefined) { // 如果是true 那么就默认当天
                jsonOption.endDate =  moment(new Date()).format('YYYY-MM-DD'); // 默认当前时间
            }
            if (jsonOption.guidParents) { // 如果存在调用子岗位接口
                this.utilityService.postData(appConfig.serverUrl  + appConfig.API.postChild, jsonOption)
                    .subscribe(
                        (val) => {
                            this.nznot.create('success', val.msg , val.msg);
                            this.getPostData();
                        },
                        (error) => {
                            this.nznot.create('error', error.msg , error.msg);
                        }
                    );
            } else { // 调用父岗位接口
                this.utilityService.postData(appConfig.serverUrl  + appConfig.API.postRoot, jsonOption)
                    .subscribe(
                        (val) => {
                            this.nznot.create('success', val.msg , val.msg);
                            this.getPostData();
                        },
                        (error) => {
                            this.nznot.create('error', error.msg , error.msg);
                        }
                    );
            }


        } else {
            this.utilityService.putData(appConfig.serverUrl  + appConfig.API.postDel, jsonOption)
                .subscribe(
                    (val) => {
                        this.getPostData();
                        this.nznot.create('success', val.msg , val.msg);
                    },
                    (error) => {
                        this.nznot.create('error', error.msg , error.msg);
                    }
                );
        }
        this.PostmodalVisible = false;
    }


    // 在岗员工数代码部分
    empDataheader = [  // 配置表头内容canel
        {value: '员工姓名' , key: 'empName', isclick: false},
        {value: '操作员', key: 'userId',  isclick:  false},
        {value: '员工状态' , key: 'empstatus', isclick: false},
        {value: '电话号码' , key: 'mobileno', isclick: false},
        {value: '入职日期' , key: 'indate', isclick: false},
    ];


    EmpData = {
        morebutton: true,
        buttons: [
            {key: 'Onboarding' , value: '入职'},
            {key: 'Departure' , value: '离职'},
            {key: 'Overview' , value: '查看概况'},
            {key: 'operator' , value: '操作员修改'},
        ]
    };

    empTotal: number; // 总页数
    empData: any; // 总数据
    showAdd: boolean; // 是否显示新增按钮
    searchOptions: any;
    selectedOption: any;

    // 初始化信息
    getEmpData(guid) {
        this.showAdd = true; // 没有新增
        this.utilityService.getData(appConfig.serverUrl + appConfig.API.listsByOrg + '/' +  this.orgGuid + '/getAssignableEmployee')
            .subscribe(
                (val) => {
                    console.log(val)
                    this.searchOptions = val.result; // 查询所有的员工，后期更改，应该查询不在岗位内的员工，接口没有好
                    // 没有在岗员工，模拟一下
                });
    }

    getEmpList(guid) {
        this.postPage = {
            condition: {
                guidOrg: this.orgGuid,
                guidPosition: guid,
            },
            page: {
                current: this.emp.pi,
                size: this.emp.size,
            }
        };
        this.utilityService.postData(appConfig.serverUrl + appConfig.API.queryByOrgPosition,  this.postPage)
            .subscribe(
                (val) => {

                    // 没有在岗员工，模拟一下  没有删除机构下岗位员工的，先用删除岗位应用的
                    for ( let i = 0; i < val.result.records.length; i++) {
                        val.result.records[i].buttonData = [{key: 'dels', value: '删除', buttonCode: appConfig.funcObject.org.orgDelApppost}];
                        val.result.records[i].indate =  moment(val.result.records[i].indate).format('YYYY-MM-DD');
                    }
                    this.empData = val.result.records;
                    this.empTotal = val.result.total;
                });
    }
    // 新增方法
    addEmp(e) {
        console.log(e);
    }
    // 顶部按钮事件
    buttonEmp(e) {
        console.log(e);
    }

    empsave() {
        this.empdistribution = false; // 弹出在岗员工数代码
        this.getPostData()
    }

    empEvent(e) {
        console.log(e)
        if (e.names) {
            if (e.names.key === 'dels') {
                this.utilityService.deleatData(appConfig.serverUrl + appConfig.API.postDelemp + '/' + e.guid + '/' + this.postGuid)
                    .subscribe(
                        (val) => {
                            this.nznot.create('success', val.msg , val.msg);
                            this.getEmpList(this.postGuid); // 重新查询列表内容
                        },
                        (error) => {
                            this.nznot.create('error', error.msg , error.msg);
                        });
            }
        }
    }

    // 翻页事件
    empHandler(e) {

    }
    // 删除事件
    deleatEmp(e) {

    }

    // 选中事件
    selectedemp() {

    }



    // 给岗位添加员工
    addEmpClick() {
        if (this.selectedOption) {
            const josnObj = {
                guidEmp: this.selectedOption,
                guidPosition: this.postGuid,
                ismain: 'N'
            }
            this.utilityService.postData(appConfig.serverUrl + appConfig.API.empAdd,  josnObj)
                .subscribe(
                    (val) => {
                        this.nznot.create('success', val.msg , val.msg);

                        this.getEmpList(this.postGuid); // 重新查询列表内容
                    },
                    (error) => {
                        this.nznot.create('error', error.msg , error.msg);
                    });
        } else {
            this.nznot.create('error', '请最起码选择一个员工进行添加' , '请最起码选择一个员工进行添加');
        }


    }

    // 岗位应用权限内容
    searchOptionse; // 选择显示的内容
    selectedMultipleOption; // 多选的内容


    array = []; // 定义数组 用来清空
    Apptotal: number; // 应用翻页
    postName: string; // 岗位名称
    appData: any[] = []; // 表格数据
    AppheaderData = [  // 配置表头内容
        { value: '应用名称', key: 'appName', isclick: false },
        { value: '应用类型', key: 'appType', isclick: false },
        { value: '应用开通时间', key: 'openDate', isclick: false },

    ];


    // 查询所有应用（已经分配之外的除外）
    getPostApp() {
        this.utilityService.getData(appConfig.serverUrl + appConfig.API.appNoth + '/' + this.postGuid)
            .subscribe(
                (val) => {
                    this.searchOptions = val.result;
                }
            );
    }

    // 查询岗位下已经有的应用列表
    getPostApplist() {
        this.postPage = {
            page: {
                current: this.pages.pi,
                size: this.pages.size,
            }
        };
        // 模拟一下
        this.utilityService.postData(appConfig.serverUrl + appConfig.API.listByPosition + '/' + this.postGuid,  this.postPage)
            .subscribe(
                (val) => {
                    for (let i = 0; i < val.result.records.length; i++ ) {
                        val.result.records[i].buttonData = [{key: 'dels', value: '删除', buttonCode: appConfig.funcObject.org.orgDelApppost}];
                        val.result.records[i].openDate =  moment(new Date()).format('YYYY-MM-DD'); // 默认当前时间
                    }
                    this.appData = val.result.records;
                    this.Apptotal = val.result.total;
                }
            );
    }


    // 给岗位新增员工
    postappAdd(event) {
        this.utilityService.postData(appConfig.serverUrl + appConfig.API.addByList,  event)
            .subscribe(
                (val) => {
                    this.nznot.create('success', val.msg , val.msg);
                    this.selectedMultipleOption = [];
                    // 重新查询岗位信息
                    this.getPostApp(); // 重新查询
                    this.getPostApplist();
                });
    }


    appClick(item) {
        if(item && item.length > 0) {
            const addApp = {
                guidPosition: this.postGuid,
                appList  : this.selectedMultipleOption,
            }
            // 调用新增接口方法
            this.postappAdd(addApp);
        } else {
            this.nznot.create('error', '没有选择应用' , '请至少选择一条应用');
        }

    }


    // 应用列表方法
    // 列表组件传过来的内容
    addappHandler(event) {
        console.log(event);
    }


    // 列表传入的翻页数据
    monitorappHandler(event) {
        console.log(event)
        this.pages.pi = event;
        this.postPage = {
            page: {
                current: event, // 页码
                size: this.pages.size, //  每页个数
            }
        };
        this.getPostApplist();
    }


    // 列表按钮方法
    buttonappDataHandler(event) {
        console.log(event); // 根据event.value来判断不同的请求，来获取结果和方法或者进行路由的跳转
    }

    selectedappRow(event) { // 选中方法，折叠层按钮显示方法
    }


    // 删除按钮
    appDel(event) {
        const jsonObj = {
            guidApp: event.guid,
            guidPosition: this.postGuid,
        }
        // 传第三表的id  event.id 即可
        this.utilityService.deleatData(appConfig.serverUrl + appConfig.API.appDelpost + '/' + event.guid + '/' + this.postGuid )
            .subscribe(
                (val) => {
                    this.nznot.create('success', val.msg , val.msg);
                    this.getPostApp()
                    this.getPostApplist();
                });

    }

    // 批量删除岗位下应用
    deleteBatchApp(event) {
        this.empdistribution = false;
        this.modal.open({
            title: '是否删除',
            content: '您是否确认删除这' + event.length + '条应用吗?',
            okText: '确定',
            cancelText: '取消',
            onOk: () => {
                let delarray = [];
                for( let i = 0; i < event.length; i++) {
                    delarray.push(event[i].guid);
                }
                let deletaObj = {
                    deleteList: delarray,
                    guid: this.postGuid
                }
                this.utilityService.postData(appConfig.serverUrl + appConfig.API.batchAppDelete, deletaObj )
                    .subscribe(
                        (val) => {
                            this.selectedRows = [];
                            this.empdistribution = true;
                            this.nznot.create('success', val.msg , val.msg);
                            this.getPostApp()
                            this.getPostApplist();
                        },
                        (error) => {
                            this.selectedRows = []
                            this.nznot.create('error', error.msg , error.msg);
                        });
            },
            onCancel: () => {
                this.selectedRows = []
                this.getPostData()
            }
        });
    }
    appsave() {
        this.empdistribution = false;
    }





}
